/**
  ******************************************************************************
  * @file    MY_Uart.c
  * @author  Hanif Rizal
  * @version V1.0.0
  * @date    18-Januari-2013
  * @brief   File ini memberikan fungsi firmware untuk mengirim dan menerima data
						 melalui UART6
  *              
  *  @verbatim
  *  
  *          ===================================================================
  *                                 How to use this driver
  *          ===================================================================              
  *   
  *  @endverbatim
  *    
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2013 Vibration Monitoring System TEAM</center></h2>
  ******************************************************************************
  */
	
/* Includes ------------------------------------------------------------------*/
#include "MY_Uart.h"

/* Global Variable -----------------------------------------------------------*/
GPIO_InitTypeDef myGPIO_InitStructure;
USART_InitTypeDef myUSART_InitStructure;

void myUSART_Configuration(void){
	
// 	 enable APB2 peripheral clock for USART6
// 	 note that only USART1 and USART6 are connected to APB2
// 	 the other USARTs are connected to APB1
	 
	// sort out clocks
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
	
	/* Configure USART6 Tx (PC.06) as alternate function push-pull */
	myGPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
	myGPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	myGPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	myGPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	myGPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOC, &myGPIO_InitStructure);
	
	// Map USART6 TX to PC.06
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
	
	/* Configure USART6 Rx (PC.07)*/
	myGPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
	myGPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_Init(GPIOC, &myGPIO_InitStructure);
	
	// Map USART6 RX to PC.07
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);
	
	// Initialize USART
	myUSART_InitStructure.USART_BaudRate = 115200;
	myUSART_InitStructure.USART_WordLength = USART_WordLength_8b;
	myUSART_InitStructure.USART_StopBits = USART_StopBits_1;
	myUSART_InitStructure.USART_Parity = USART_Parity_No;
	myUSART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	myUSART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	/* Configure USART */
	USART_Init(USART6, &myUSART_InitStructure);
	/* Enable the USART */
	USART_Cmd(USART6, ENABLE);
}

/* This function is used to transmit a string of characters via
 * the USART specified in USARTx.
 *
 * It takes two arguments: USARTx --> can be any of the USARTs e.g. USART1, USART2 etc.
 * 						   (volatile) char *s is the string you want to send
 *
 * Note: The string has to be passed to the function as a pointer because
 * 		 the compiler doesn't know the 'string' data type. In standard
 * 		 C a string is just an array of characters
 *
 * Note 2: At the moment it takes a volatile char because the received_string variable
 * 		   declared as volatile char --> otherwise the compiler will spit out warnings
 * */
void USART_puts(USART_TypeDef* USARTx, volatile char *s){

	while(*s){
		// wait until data register is empty
		while( !(USARTx->SR & 0x00000040) );
		USART_SendData(USARTx, *s);
		*s++;
	}
}
/******************* (C) COPYRIGHT 2013 Hanif Rizal *****END OF FILE****/

